package com.cakeclient.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class DataSource {
    private Context context;
    private OurSQLiteOpenHelper dbHelper;
    private SQLiteDatabase database;


    public DataSource(Context context) {
        this.context = context;
    }

    public DataSource open() throws SQLException {
        this.dbHelper = new OurSQLiteOpenHelper(context);
        this.database = this.dbHelper.getWritableDatabase();
        return this;
    }


    public void close() {
        try {
            dbHelper.close();
        } catch (Exception e) {

        }
    }


    public long insert(IDatabasable databasable, DatabaseTable databaseTable) {
        databasable.setIdDB(database.insertOrThrow(databaseTable.getTableName(), null, databasable.getValues()));
        return databasable.getIdDB();
    }

    public long insert(DatabaseTable databaseTable, Long LeftId, Long RightId) {
        ContentValues values = new ContentValues();
        values.put(databaseTable.getAllFields()[1], LeftId);
        values.put(databaseTable.getAllFields()[2], RightId);
        return database.insertOrThrow(databaseTable.getTableName(), null, values);
    }

    public long update(IDatabasable databasable, DatabaseTable databaseTable) {
        return database.update(databaseTable.getTableName(), databasable.getValues(), "_id " + " = ?", new String[]{databasable.getIdDB().toString()});
    }

    public long delete(IDatabasable databasable, DatabaseTable databaseTable) {
        return database.delete(databaseTable.getTableName(), "_id " + " = ?", new String[]{databasable.getIdDB().toString()});
    }

    public long delete(IDatabasable databasable, DatabaseTable databaseTable, String WhereColumn) {
        return database.delete(databaseTable.getTableName(), WhereColumn + " = ?", new String[]{databasable.getIdDB().toString()});
    }

    public Cursor fetchOne(Long idDB, DatabaseTable databaseTable) {
        return database.query(databaseTable.getTableName(), databaseTable.getAllFields(), "_id " + " = ?", new String[]{idDB.toString()}, null, null, null);
    }

    public Cursor getMore(IDatabasable Databasable, DatabaseTable databaseTable, String WhereColumn) {
        return database.query(databaseTable.getTableName(), databaseTable.getAllFields(), WhereColumn + " = ?", new String[]{Databasable.getIdDB().toString()}, null, null, null);
    }

    public Cursor fetchAll(DatabaseTable databaseTable) {
        return database.query(databaseTable.getTableName(), databaseTable.getAllFields(), null, null, null, null, null);
    }

    public Cursor fetchOneFromRemoteID(Long remoteID, DatabaseTable databaseTable) {
        return database.query(databaseTable.getTableName(), databaseTable.getAllFields(), "remoteId " + " = ?", new String[]{remoteID.toString()}, null, null, null);
    }

}
